package interview.recursion;

import interview.common.Node;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description: 递归
 * User: xxm
 * Date: 2018-02-18
 * Time: 14:59
 */
public class LinkedListCreator {

    /**
     * 创建一个单链表
     *
     * @param data data the data to create the list
     * @return head of the linked list.
     * The returned linked list ends with last node getNext() == null.
     */
    public Node createLinkedList(List<Integer> data) {
        if (data.isEmpty()) {
            return null;
        }

        Node head = new Node(data.get(0));
        Node linkedList = createLinkedList(data.subList(1, data.size()));// subList: 若起始下标等于截止下标则返回null
        head.setNext(linkedList);
        return head;
    }

    public static void main(String[] args) {
        LinkedListCreator creator = new LinkedListCreator();

        Node.printLinkedList(
                creator.createLinkedList(new ArrayList<Integer>())
        );

        Node.printLinkedList(
                creator.createLinkedList(Arrays.asList(1))
        );

        Node.printLinkedList(
                creator.createLinkedList(Arrays.asList(1, 2, 3, 4, 5))
        );

    }

}
